package cn.agiledata.bank.linkbank.impl.validate;

import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.logic.FieldValidator;

public abstract class AbstractFieldValidator implements FieldValidator {

    private String target;

    private boolean nullable;

    private boolean emptiable;

    private String errorCode;

    protected String getErrorCode() {
        return this.errorCode;
    }

    public String getTarget() {
        return this.target;
    }

    public boolean isEmptiable() {
        return this.emptiable;
    }

    protected boolean isNullable() {
        return this.nullable;
    }

    public void setEmptiable(boolean emptiable) {
        this.emptiable = emptiable;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    protected void validateEmptiable(String value) throws LinkBankException {
        if ("".equals(value) && !isEmptiable()) {
            throw LinkBankException.getEmptyException(getTarget(),
                    getErrorCode());
        }
    }

    protected void validateNullable(String value) throws LinkBankException {
        if (value == null && !isNullable()) {
            throw LinkBankException.getNullException(getTarget(),
                    getErrorCode());
        }
    }

}